import React, {
  createContext,
  useContext,
  useEffect,
  useRef,
  useState,
} from "react";
import { new_session } from "@/utils/common";
import store from "store2";
import { intervalRefreshToken } from "@/services/supcon-chat-tool/api/app";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
let time_out = 6000;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let refresh_loop_time = 3000;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let refresh_time = 0;
let intervalTimer: any = null;

const GlobalDataContext = createContext<any>(null);

export function useGlobalData() {
  return useContext(GlobalDataContext);
}

export function GlobalDataProvider({ children }: { children: any }) {
  let storeUserInfo = store.get("userInfo");
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const [theme, setTheme] = useState<ThemeType>("light");
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const [language, setLanguage] = useState<AppLanguageType>("zh-CN");

  const [globalData, setGlobalData] = useState({});
  const [userInfo, setUserInfo] = useState<any>(storeUserInfo);

  // 底部ref, 用来控制 底部切换 等场景
  const chatInputFooterRef = useRef<any>(null);

  // 场景: 目前对应 h5 来说全是 text_chat
  const [chatScene, setChatScene] = useState<ChatSceneType>("" as any);
  const [counter, setCounter] = useState(0);
  // 深度思考状态
  const [think, setThink] = useState(false);

  // 行业知识
  const [hyzsTarget, setHyzsTarget] = useState(false);

  // 底部额外操作栏
  const [footerExtraOpVisible, setFooterExtraOpVisible] = useState(false);

  // 具体的弹出层: 这个不能用 全局的 chatScene 代替,因为 chatScene 总是存在,但是这个类似 visible, 只有当点击的时候才显示
  // '' | 'default'|'knowledgeBase' |'chatBI'
  const [footerExtraOpPupVisibleContent, setFooterExtraOpPupVisibleContent] =
    useState<any>({});
  // 展示插件弹窗, 和上面区分, footerExtraPupVisible 会一直改变 是否开启
  const [footerExtraPupVisible, setFooterExtraPupVisible] = useState(false);

  // 思维链弹窗
  const [siweiVisible, setSiweiVisible] = useState<{
    visible: boolean;
    type: "chat" | "view";
  }>({
    visible: false,
    // chat:从 viewcontent  获取, view: 从siweiContent 获取;
    type: "view",
  });
  const [siweiContent, setSiweiContent] = useState("");

  const [chatMemo, setChatMemo] = useState<any>({});
  const chatMemoRef = useRef<any>({});
  chatMemoRef.current = chatMemo;

  const clearChatMemo = () => {
    setFooterExtraOpPupVisibleContent({});
    setChatMemo({});
    chatMemoRef.current = {};
  };

  const handleChangeChatMemo = (k = "", v = {}, trueId?: string) => {
    let cMemo = { ...chatMemoRef.current };
    if (trueId) {
      let sItem = cMemo[new_session] ?? {};
      cMemo[trueId] = {
        ...sItem,
        ...v,
      };
    } else {
      cMemo[k] = { ...v };
    }
    setChatMemo(cMemo);
    chatMemoRef.current = cMemo;
  };

  useEffect(() => {
    //  刷新token
    intervalRefreshToken();
  }, []);

  useEffect(() => {
    return () => {
      clearInterval(intervalTimer);
    };
  }, []);

  // 提供全局数据的 setter 函数
  const setGlobal = (data: any) => {
    setGlobalData((prevGlobalData) => ({
      ...prevGlobalData,
      ...data,
    }));
  };

  return (
    <GlobalDataContext.Provider
      value={{
        theme,
        language,
        ...globalData,
        setGlobal,
        userInfo,
        setUserInfo,
        // refreshInterval,
        chatScene,
        setChatScene,
        chatMemo,
        chatMemoRef,
        handleChangeChatMemo,
        footerExtraOpPupVisibleContent,
        setFooterExtraOpPupVisibleContent,
        footerExtraPupVisible,
        setFooterExtraPupVisible,
        think,
        setThink,
        footerExtraOpVisible,
        setFooterExtraOpVisible,
        counter,
        setCounter,
        setChatMemo,
        clearChatMemo,
        chatInputFooterRef,
        siweiVisible,
        setSiweiVisible,
        siweiContent,
        setSiweiContent,
        hyzsTarget,
        setHyzsTarget,
      }}
    >
      {children}
    </GlobalDataContext.Provider>
  );
}
